package org.jeecg.modules.htxincailiao.forums.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RequestUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.account.entity.HTForumsExpert;
import org.jeecg.modules.htxincailiao.account.service.IHTForumsExpertService;
import org.jeecg.modules.htxincailiao.forums.entity.HTForumsExpertWorks;
import org.jeecg.modules.htxincailiao.forums.service.IHTForumsExpertWorksService;
import org.jeecg.modules.permissions.HTRolePermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: ht_forums_expert_works
 * @Author: jeecg-boot
 * @Date: 2022-04-12 15:32:01
 * @Version: V1.0
 */
@Api(tags = "ht_forums_expert_works")
@RestController
@RequestMapping("/forums/hTForumsExpertWorks")
@Slf4j
public class HTForumsExpertWorksController extends JeecgController<HTForumsExpertWorks, IHTForumsExpertWorksService> {
    @Resource
    private IHTForumsExpertWorksService hTForumsExpertWorksService;

    @Resource
    private IHTForumsExpertService forumsExpertService;

    /**
     * 分页列表查询
     *
     * @param hTForumsExpertWorks
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "ht_forums_expert_works-分页列表查询")
    @ApiOperation(value = "ht_forums_expert_works-分页列表查询", notes = "ht_forums_expert_works-分页列表查询")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> queryPageList(HTForumsExpertWorks hTForumsExpertWorks,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {


        if (req.getMethod().equals("POST")) {
            String line = RequestUtil.getPostParam(req);
            if (oConvertUtils.isNotEmpty(line)) {
                hTForumsExpertWorks = JSONObject.parseObject(line, HTForumsExpertWorks.class);
                pageNo = oConvertUtils.isNotEmpty(hTForumsExpertWorks.getPageNo()) ? hTForumsExpertWorks.getPageNo() : pageNo;
                pageSize = oConvertUtils.isNotEmpty(hTForumsExpertWorks.getPageSize()) ? hTForumsExpertWorks.getPageSize() : pageSize;
            }
        }

        if (oConvertUtils.isNotEmpty(hTForumsExpertWorks) &&
                oConvertUtils.isNotEmpty(hTForumsExpertWorks.getForumsCategoryId()) &&
                hTForumsExpertWorks.getForumsCategoryId().equals("0")) {
            //全部分类
            hTForumsExpertWorks.setForumsCategoryId(null);
        }
        String loginUserForumsExpertId = HTRolePermission.GetLoginUserForumsExpertId(req);
        if(oConvertUtils.isNotEmpty(loginUserForumsExpertId)){
            hTForumsExpertWorks.setForumsExpertId(loginUserForumsExpertId);
        }

        QueryWrapper<HTForumsExpertWorks> queryWrapper = QueryGenerator.initQueryWrapper(hTForumsExpertWorks, req.getParameterMap());
        Page<HTForumsExpertWorks> page = new Page<HTForumsExpertWorks>(pageNo, pageSize);
        IPage<HTForumsExpertWorks> pageList = hTForumsExpertWorksService.page(page, queryWrapper);
        List<HTForumsExpertWorks> records = pageList.getRecords();
        records.forEach(record -> {
            String forumsExpertId = record.getForumsExpertId();
            if (oConvertUtils.isNotEmpty(forumsExpertId)) {
                HTForumsExpert forumsExpert = forumsExpertService.getById(forumsExpertId);
                if (oConvertUtils.isNotEmpty(forumsExpert)) {
                    if (oConvertUtils.isNotEmpty(forumsExpert.getRealName()))
                        record.setForumsExpertRealName(forumsExpert.getRealName());

                    if (oConvertUtils.isNotEmpty(forumsExpert.getPhoneNumber()))
                        record.setForumsExpertPhoneNumber(forumsExpert.getPhoneNumber());
                }
            }
        });

        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param hTForumsExpertWorks
     * @return
     */
    @AutoLog(value = "ht_forums_expert_works-添加")
    @ApiOperation(value = "ht_forums_expert_works-添加", notes = "ht_forums_expert_works-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody HTForumsExpertWorks hTForumsExpertWorks) {
        hTForumsExpertWorksService.save(hTForumsExpertWorks);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param hTForumsExpertWorks
     * @return
     */
    @AutoLog(value = "ht_forums_expert_works-编辑")
    @ApiOperation(value = "ht_forums_expert_works-编辑", notes = "ht_forums_expert_works-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HTForumsExpertWorks hTForumsExpertWorks) {
        hTForumsExpertWorksService.updateById(hTForumsExpertWorks);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_forums_expert_works-通过id删除")
    @ApiOperation(value = "ht_forums_expert_works-通过id删除", notes = "ht_forums_expert_works-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        hTForumsExpertWorksService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "ht_forums_expert_works-批量删除")
    @ApiOperation(value = "ht_forums_expert_works-批量删除", notes = "ht_forums_expert_works-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hTForumsExpertWorksService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_forums_expert_works-通过id查询")
    @ApiOperation(value = "ht_forums_expert_works-通过id查询", notes = "ht_forums_expert_works-通过id查询")
    @RequestMapping(value = "/queryById", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> queryById(HttpServletRequest req, @RequestParam(name = "id", required = false) String id) {
        if (req.getMethod().equals("POST")) {
            String line = RequestUtil.getPostParam(req);
            if (oConvertUtils.isNotEmpty(line)) {
                HTForumsExpertWorks hTForumsExpertWorks = JSONObject.parseObject(line, HTForumsExpertWorks.class);
                id = hTForumsExpertWorks.getId();
            }
        }

        HTForumsExpertWorks hTForumsExpertWorks = hTForumsExpertWorksService.getById(id);
        if (hTForumsExpertWorks == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hTForumsExpertWorks);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param hTForumsExpertWorks
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HTForumsExpertWorks hTForumsExpertWorks) {
        return super.exportXls(request, hTForumsExpertWorks, HTForumsExpertWorks.class, "ht_forums_expert_works");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HTForumsExpertWorks.class);
    }

}
